<?php

namespace App\Console\Commands;

use App\Models\User;
use Illuminate\Console\Command;
use DB;

class CalculateUserVipTime extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'book:calculate-user-vip-time';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '计算用户会员时长';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->info("开始获取...");

        $now = time();

        $users = DB::table('users')->where('user_type',1)->where('is_vip',1)->get();
        if(!empty($users))
        {
            foreach ($users as $v)
            {
                if(!empty($v->vip_end_time) && !empty($v->vip_start_time))
                {
                    $sms = app('easysms');
                    if($v->vip_end_time < $now)
                    {
                        //已经过期
                        DB::table('users')->where('id',$v->id)->update(['is_vip'=>-1,'vip_start_time'=>0,'vip_end_time'=>0]);
                        //发送短信
                        try {
                            $sms->send($v->telephone,[
                                'template' => 'SMS_142949811',
                                'data' => [
                                    'name' => $v->wechat_nickname
                                ],
                            ]);
                        } catch (\Overtrue\EasySms\Exceptions\NoGatewayAvailableException $exception) {
                            $message = $exception->getException('aliyun')->getMessage();
                            dd($message);
                        }
                    }
                    else
                    {
                        //$timediff = $v->vip_end_time - $v->vip_start_time;
                        $timediff = $v->vip_end_time - $now;
                        $day = ceil($timediff/86400);
                        

                        if($day == 10)
                        {
                            //还剩10天提醒
                            try {
                                $sms->send($v->telephone,[
                                    'template' => 'SMS_142383201',
                                    'data' => [
                                        'name' => $v->wechat_nickname
                                    ],
                                ]);
                            } catch (\Overtrue\EasySms\Exceptions\NoGatewayAvailableException $exception) {
                                $message = $exception->getException('aliyun')->getMessage();
                                dd($message);
                            }
                        }

                        if($day == 7)
                        {
                            //还剩7天提醒
                            try {
                                $sms->send($v->telephone,[
                                    'template' => 'SMS_148381598',
                                    'data' => [
                                        'name' => $v->wechat_nickname
                                    ],
                                ]);
                            } catch (\Overtrue\EasySms\Exceptions\NoGatewayAvailableException $exception) {
                                $message = $exception->getException('aliyun')->getMessage();
                                dd($message);
                            }
                        }

                    }
                }
            }
        }
        $this->info("成功获取...");
    }
}
